<?php

namespace App\Http\Controllers;

use App\Models\Associator;
use Illuminate\Http\Request;
use App\Handlers\ImageUploadHandler;
use App\Http\Requests\UserRequest;
use Zhuzhichao\BankCardInfo\BankCard;

class UsersController extends Controller
{
    public function __construct()
    {
//        dd(BankCard::getBankList());
        $this->middleware('auth', [
            'except' => ['login', 'store']
        ]);
        $this->middleware('guest', [
            'only' => ['store','login']
        ]);
    }
    public function login(){
        return view('users/login');
    }

    public function store(Request $request){
        $user = Associator::create(
            [
                'u_id' => uniqid(),
                'u_name' => $request['name'],
                'mobile' => $request['mobile'],
                'password' => bcrypt($request['password']),
                'current_status' => '0'
            ]
        );
        return redirect()->route('users.show', [$user]);
    }

    public function show(Associator $user){
        $this->authorize('update', $user);
        return view('users.grziliao',compact('user'));
    }
    public function safe_center(Associator $user){
        $this->authorize('update', $user);
        return view('users.aqzhongxin',compact('user'));
    }
    public function sqrz(Associator $user){
        $this->authorize('update', $user);
        return view('users.sqruzhu',compact('user'));
    }
    public function liwu(Associator $user){
        $this->authorize('update', $user);
        return view('users.liwujilu',compact('user'));
    }
    public function rexian(Associator $user){
        $this->authorize('update', $user);
        return view('users.rexianjilu',compact('user'));
    }
    public function update(Associator $user, ImageUploadHandler $uploader, UserRequest $request){
        $this->authorize('update',$user);
        $data = $request->all();
        if ($request->portrait) {
            $result = $uploader->save($request->portrait, 'avatars', $user->id,362);
            if ($result) {
                $data['portrait'] = $result['path'];
            }
        }
        $user->update($data);
        return redirect()->route('users.show', [$user])->with('msg','修改成功！');
    }
}
